import Vue from 'vue'
import App from './App'
import router from './router'
import store from './vuex/index'
import request from './js/request'
import {remChange} from './js/remChange'
import { wxConfig, updateShare } from './js/wxenv'
import './css/common.scss'
Vue.use(request)
remChange()
Vue.config.productionTip = false
router.beforeEach((to, from, next) => {
  document.querySelector('title').innerHTML = to.name
  next()
})

let touchEndTime
window.addEventListener('touchend', () => {
  touchEndTime = Date.now()
})

router.beforeEach((to, from, next) => {
  // ios滑动切换
  if ((Date.now() - touchEndTime) > 50 && (Date.now() - touchEndTime) < 377) {
    store.commit('routerAni', '')
  } else {
    const toPathLen = to.path.length
    const fromPathLen = from.path.length
    let routerAni = toPathLen < fromPathLen ? 'slide-right' : 'slide-left'
    store.commit('routerAni', routerAni)
  }
  document.querySelector('title').innerHTML = to.name
  next()
})
// const sleep = time => {
//   return new Promise((resolve, reject) => {
//     setTimeout(resolve, time)
//   })
// }
// const test = async () => {
//   for (let i = 0; i < 10000; i++) {
//     await sleep(50)
//     console.log(`第${i}次请求`)
//     wxConfig()
//   }
// }
// test()
router.afterEach(to => {
  // wxConfig(() => updateShare())
  if (window.WeixinJSBridge) {
    wxConfig(() => updateShare())
  } else {
    document.addEventListener('WeixinJSBridgeReady', () => {
      // alert('WeixinJSBridge is ready')
      console.log('WeixinJSBridge is ready')
      wxConfig(() => updateShare())
    }, false)
  }
})
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  template: '<App/>',
  components: { App }
})
